package com.xc.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xc.base.model.PageParams;
import com.xc.base.model.PageResult;
import com.xc.content.mapper.CourseBaseMapper;
import com.xc.content.model.dto.QueryCourseParamsDto;
import com.xc.content.model.po.CourseBase;
import com.xc.content.service.CourseBaseInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author : LdLtd
 * @Date : 2023/6/5
 * @Description:
 */
@Slf4j
@Service
public class CourseBaseInfoServiceImpl implements CourseBaseInfoService {
    @Autowired
    CourseBaseMapper courseBaseMapper;


    @Override
    public PageResult<CourseBase> queryCourseBaseList(PageParams pageParams, QueryCourseParamsDto queryCourseParamsDto) {
        //1.构造查询条件对象
        LambdaQueryWrapper<CourseBase> queryWrapper = new LambdaQueryWrapper<>();
        //构造查询条件，根据课程名称查询
        queryWrapper
                .like(
                        StringUtils.isNotBlank(queryCourseParamsDto.getCourseName()),
                        CourseBase::getName,
                        queryCourseParamsDto.getCourseName())
                //构造查询条件，根据课程审核状态查询
                .eq(
                        StringUtils.isNotBlank(queryCourseParamsDto.getAuditStatus()),
                        CourseBase::getAuditStatus,
                        queryCourseParamsDto.getAuditStatus())
                //构造查询条件,根据课程发布状态查询
                .eq(
                        StringUtils.isNotBlank(queryCourseParamsDto.getPublishStatus()),
                        CourseBase::getStatus,
                        queryCourseParamsDto.getPublishStatus());

       //分页对象
        Page<CourseBase> page = new Page<>(pageParams.getPageNo(), pageParams.getPageSize());
        //查询数据内容获取结果
        Page<CourseBase> pageResut = courseBaseMapper.selectPage(page, queryWrapper);
        //获取数据列表
        List<CourseBase> list = pageResut.getRecords();
        //获取数据总数
        long total = pageResut.getTotal();
        //构造结果集
        PageResult<CourseBase> courseBasePageResult = new PageResult<>(list, total, pageParams.getPageNo(), pageParams.getPageSize());
        //返回
        return courseBasePageResult;
    }
}
